import React from 'react'
import { withRouter } from 'react-router-dom'

Prompt.defaultProps = {
    when: false, // true 表示添加阻塞
    message: 'Are you sure you want to leave this page?',
}

 function Prompt() {

    const unBlockRef = useRef(null)


    const handleBlock = () => {
        if (props.when) {
            unBlockRef.current = history.block(props.message)
        } else {
            if (unBlockRef.current) {
                unBlockRef.current()
            }
        }
    }

    useEffect(() => {
        handleBlock()

        return () => {
            if (unBlockRef.current) {
                unBlockRef.current()
            }
        }
    }, [props.when])


    return null
}

export default withRouter(Prompt)